package com.debao;

import com.debao.common.core.domain.AjaxResult;
import com.debao.common.utils.SecurityUtils;
import com.debao.system.domain.Collect;
import com.debao.system.service.ICollectService;
import com.debao.system.service.IInterestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.debao.common.core.domain.AjaxResult.error;
import static com.debao.common.core.domain.AjaxResult.success;

@CrossOrigin
@RestController
@RequestMapping("/api/collect")
public class ApiCollect {

    @Autowired
    private ICollectService collectService;

    /**
     * 我的收藏列表
     * @param type
     * @return
     */
    @GetMapping("/list/{type}")
    public AjaxResult list(String type) {
        List<Collect> collects = collectService.selectCollectList(new Collect().setUserId(SecurityUtils.getUserId()).setType(type));
        return AjaxResult.success(collects);
    }


    /**
     * 收藏
     */
    @PostMapping(value = "addCollect/{otherId}")
    public AjaxResult addCollect(@PathVariable("otherId") Long otherId) {
        int row = collectService.insertCollect(new Collect().setUserId(SecurityUtils.getUserId()).setOtherId(otherId).setType("S"));
        return row == 1 ? success("收藏成功") : error("已收藏");
    }

    /**
     * 取消收藏
     *
     * @param otherId
     * @return
     */
    @DeleteMapping(value = "delCollect/{otherId}")
    public AjaxResult delCollect(@PathVariable("otherId") Long otherId) {
        collectService.deleteCollect(SecurityUtils.getUserId(), otherId, "S");
        return success("已取消收藏");
    }

}
